var ws, spage, aid, isrepeat, isshock, uid, hours, minutes, lingbell, parentPg;
var ntime, h, m, w;//系统时间的几点几分的总分数、时、分、秒、星期
var wk;//今天星期几
var ttime;//用户设置的总分钟数
var ww;//用户设置的星期的集合
document.addEventListener('plusready',function(){
	uid = plus.storage.getItem('uid');
	ws = plus.webview.currentWebview();
	parentPg = plus.webview.getWebviewById('index.html');
	spage = ws.spage;
	aid = ws.aid;
//	console.log(aid+" "+spage);
	var wlist = plus.storage.getItem('warnlist');
	var data = $.parseJSON(wlist); 
	$.each(data, function(k,v) {
		if(v.id == aid){
			plus.nativeUI.closeWaiting();
			console.log(JSON.stringify(v.bell)); 
			hours = parseInt(v.hours);
			minutes = parseInt(v.minutes);
			ttime = (hours*60) + minutes//总分钟
			ww = v.week.split(' '); //用户选择的星期-》用空格隔开
			Countdown();
			$('#week').text(v.week);
			$('.cpills').val(v.takepills);
			$('.cpill').text(v.takepills);
			$('.fpills').val(v.familypills);
			$('.fpill').text(v.familypills);
			$('.hours').text(v.hours);
			$('.minutes').text(v.minutes);
			v.bell?$('.lingsheng').text(v.bell):$('.lingsheng').text('默认铃声');
			lingbell = v.bell;
			if(v.isrepeat == 1){
				$('.chongfu').addClass('mui-active');
				isrepeat = 1;
			}else{
				isrepeat = 0;
			}
			if(v.isshock == 1){
				$('.zhendong').addClass('mui-active');
				isshock = 1;
				$('.shock').text('震动');
			}else{
				isshock = 0;
				$('.shock').text('关闭震动');
			}
			var wek = v.week.split(' ');
			for(var i = 0; i < wek.length - 1; i++){
				$('.xue:contains('+wek[i]+')').next('input').attr('checked',true);
//				console.log(wek[i]);
			}
		}
	})
	//是否重复
	$('.chongfu').on('click',function(){
		if(this.classList.contains('mui-active')){//开启
			isrepeat = 1;
		}else{//关闭
			isrepeat = 0;
		}
	})
	$('.csure').on('tap', function(){
		$('.cpill').text($('.cpills').val());
	})
	$('.fsure').on('tap', function(){
		$('.fpill').text($('.fpills').val());
	})
	//是否震动
	$('.zhendong').on('tap',function(){
		if(this.classList.contains('mui-active')){//开启
			isshock = 1;
		}else{//关闭
			isshock = 0;
		}
	})
	//星期选择
	$('.wek .we').on('tap',function(){
		var week = '';
		$('input[name=radio1]:checked').each(function(){
			week += $(this).siblings('label').text()+' ';
		})
		console.log(week);
		week?$('#week').text(week):$('#week').text('选择星期');
	})
	//选择铃声
	$('.bell').click(function(){
		goNewPage('me-remind-bell.html',{spage:ws.id, lingbell:lingbell})
	})
	//点击完成
	$('.overWarn').on('tap',function(){
		var xingqi = $('#week').text();
		var cpills = $('.cpills').val();//吃药注意事项
		var fpills = $('.fpills').val();//家庭成员注意事项
		var hours = $('.hours').text();//选择的时
		var minutes = $('.minutes').text();//选择的分
		var bell = $('.lingsheng').text();//铃声
		console.log(xingqi+" "+cpills+" "+fpills+" "+hours+" "+minutes+" "+isrepeat+" "+isshock+" "+bell);
		if(xingqi == '选择星期' || !cpills.trim() || !fpills.trim() || !hours.trim() || !minutes.trim()){
			toast('请补全信息');return;
		}
		showWating();
		$.ajax({
			type:"get",
			url:apiRoot+"?m=home&c=medicinal&a=saveWarn",
			data :{
				week : xingqi,
				takepills :cpills,
				familypills :fpills,
				hours :hours, 
				minutes :minutes,
				isrepeat :isrepeat,
				isshock :isshock,
				bell :bell,
				id :aid
			},
			dataType :'json',
			success :function(data){
				plus.nativeUI.closeWaiting();
				console.log(JSON.stringify(data));
				if(data > 0){
					toast('修改成功');
					parentPg.evalJS('searchWarn()');
					if(spage){
						plus.webview.getWebviewById(spage).reload(); 
					}
					setTimeout(function(){
						ws.close();
					},2000);
				}
			},
			error :function(e){
				console.log(JSON.stringify(e));
				errortoast();
			}
		});
	})
},false)
//倒计时
var vk;//下一个星期数
function Countdown(){
	//获取当前 时 分
	d = new Date(); //实例一个时间对象；
	h = parseInt(d.getHours()); //获取系统时，
	m = parseInt(d.getMinutes()); //分
	w = parseInt(d.getDay());//星期
	switch(w){
		case 0:wk = '星期日';break;
		case 1:wk = '星期一';break;
		case 2:wk = '星期二';break;
		case 3:wk = '星期三';break;
		case 4:wk = '星期四';break;
		case 5:wk = '星期五';break;
		case 6:wk = '星期六';break;
	}
	ntime = (h*60) + m//现在总分钟
	var jtime = ttime - ntime;//相减的分钟数
	
	if(w == 0)w=7;//w当天星期数
//	console.log(w+" "+vk);
	var tian = 0;
	
	var x = [];//用户选的星期数组
	for(var i = 0; i < ww.length -1; i++){ 
		switch(ww[i]){
			case '星期日':vk = 0;x.push(vk);break;//vk星期数
			case '星期一':vk = 1;x.push(vk);break;
			case '星期二':vk = 2;x.push(vk);break;
			case '星期三':vk = 3;x.push(vk);break;
			case '星期四':vk = 4;x.push(vk);break;
			case '星期五':vk = 5;x.push(vk);break;
			case '星期六':vk = 6;x.push(vk);break;
		}
	} 
	for(var i=0;i<x.length;i++){
		console.log(x[i]);
		if(x[i] == w){
			if(jtime < 0){
				/**
				 * 有今天，但是时间已经过了
				 * 在原有的基础上减1天
				 */
				tian = 7 - w + x[i];
			}else{
				tian = 0;
			}
			break;
		}else if(x[i] > w){
			tian = x[i] - w;//相差几天
			break;
		}else if(x[i] < w){
			tian = 7 - w +x[i]; 
//				break;
		}
		if(!x[i+1]){ 
//				x[i] = x[0];
			tian = 7 - w +x[0];break;
		}//若是最后一个则返回第一个
    }
	console.log(w+" "+tian);
	var next_sz = tian != '0'?(tian - 1) * 60 * 24 + ( 24 * 60 - ntime) + ttime:(7 - 1) * 60 * 24 + ( 24 * 60 - ntime) + ttime;
//	var next_sz = (tian - 1) * 60 * 24 + ( 24 * 60 - ntime) + ttime;
//		console.log(tian+" "+next_sz);
	//先求天  
	var s_day = parseInt(next_sz/60/24);
	//再求时
	var sc = (next_sz -  s_day * 60 *24)/60;
	var s_hour = parseInt(sc); 
	//最后求分钟
	var s_minute = next_sz - parseInt(s_day * 60 *24) - (parseInt((next_sz -  s_day * 60 *24)/60) * 60);
//		console.log(s_day+" "+s_hour+" "+s_minute); 
	
	if(wk == ww[i]){//当星期有符合该条提醒的星期
		if(jtime == 0){//今天的时间和用户时间一致，也就是时间到了，则提醒
//			plus.device.vibrate(1000);//震动 
			var mm = '0天0小时0分钟';
//			clearInterval(cc);
//			cc = setInterval(Countdown,60000);
		}else if(jtime < 0){//时间已过 
			mm = s_day + "天" + s_hour + "小时" + s_minute + "分";
		}else if(jtime > 0){
			//今日响铃还未到
			mm = jtime/60 >= 1?parseInt(jtime/60)+'小时'+(jtime - (parseInt(jtime/60)*60))+'分钟后响铃':'0小时'+jtime+"分钟后响铃";
		}
	}else{
		mm = s_day + "天" + s_hour + "小时" + s_minute + "分"; 
	}
		
	
	console.log(mm);
//	if(mm == '0天0小时0分钟'){
//		mm = '提醒中';
//		isshock == 1?plus.device.vibrate(2000):'';//震动时长
//		plus.device.setWakelock( true );//亮屏
//		clearInterval(tt); 
//	}
	$('.remind-list-time').text(mm);
}
var cc = setInterval(Countdown,60000);

window.addEventListener('bell',function(event){
	var bell = event.detail.bell;
	console.log(bell);
	$('.lingsheng').text(bell);
},false)